﻿define(function (require, exports, module) {
    var ImageStorage = require('util/imageStorage');
    var Canvas = require('util/canvas');
    var Config = require('util/config');

    var m_imageBasePath = 'images\\character';

    function Character(data) {
        this.data = data;
        this.changeAction(Config.character.action.STAND);
    };

    Character.prototype.stand = function () {
        this.changeAction(Config.character.action.STAND);
    };

    Character.prototype.move = function () {
        this.changeAction(Config.character.action.MOVE);
    };

    Character.prototype.changeAction = function (action) {
        this.action = action;
        this.data.actionId = this.action.id;
        resetActionImagePaths(this);
    };

    Character.prototype.steering = function () {
        Config.character.direction.SOUTH_EAST < ++this.data.directionId && (this.data.directionId = Config.character.direction.SOUTH);
        resetActionImagePaths(this);
    };

    Character.prototype.changeColor = function () {
        Config.character.color.GOLD < ++this.data.colorId && (this.data.colorId = Config.character.color.GREEN);
        resetActionImagePaths(this);
    };

    Character.prototype.getHeadBgPath = function () {
        return [m_imageBasePath, this.data.characterId, this.data.colorId, 'head.png'].join('\\');
    };

    Character.prototype.getExpressionUpPath = function () {
        return resetExpressionAndGetPath(this, 'Up');
    };

    Character.prototype.getExpressionDownPath = function () {
        return resetExpressionAndGetPath(this, 'Down');
    };

    //name: Up or Down
    function resetExpressionAndGetPath(character, name) {
        var property = 'expression' + name + 'Id', lower = name.toLowerCase();
        if (character.data[property] < Config.character.expression[lower].FIRST)
            character.data[property] = Config.character.expression[lower].FIFTH;
        else if (Config.character.expression[lower].FIFTH < character.data[property])
            character.data[property] = Config.character.expression[lower].FIRST;
        return [m_imageBasePath, character.data.characterId, character.data[property] + '.png'].join('\\');
    };

    function resetActionImagePaths(character) {
        character.actionPaths = [];
        var imageBasePath = [
            m_imageBasePath,
            character.data.characterId,
            character.data.colorId,
            character.data.handId,
            character.data.directionId,
            character.data.actionId,
            ''].join('\\');
        for (var i = 1; i <= character.action.count; i++) {
            character.actionPaths.push([imageBasePath, i, '.png'].join(''));
        }
    };

    module.exports = Character;
});